<script setup>
import { ref } from 'vue'

// 默认快捷链接
const quickLinks = ref([
  { name: 'Gitee', url: 'https://gitee.com', icon: '💻' },
  { name: 'GitHub', url: 'https://github.com', icon: '🐙' },
  { name: '知乎', url: 'https://zhihu.com', icon: '💡' },
  { name: 'B站', url: 'https://bilibili.com', icon: '📺' },
  { name: '微信读书', url: 'https://weread.qq.com', icon: '📚' },
  { name: '网易云音乐', url: 'https://music.163.com', icon: '🎵' },
  { name: '淘宝', url: 'https://taobao.com', icon: '🛒' },
  { name: '京东', url: 'https://jd.com', icon: '📦' }
])

// 添加新链接
const newLink = ref({ name: '', url: '', icon: '🔗' })
const showAddForm = ref(false)

const addLink = () => {
  if (newLink.value.name && newLink.value.url) {
    // 确保URL以http://或https://开头
    let url = newLink.value.url
    if (!url.startsWith('http://') && !url.startsWith('https://')) {
      url = 'https://' + url
    }
    
    quickLinks.value.push({ 
      ...newLink.value, 
      url: url 
    })
    newLink.value = { name: '', url: '', icon: '🔗' }
    showAddForm.value = false
  }
}

// 验证URL格式
const validateUrl = (url) => {
  try {
    new URL(url)
    return true
  } catch {
    return false
  }
}

const removeLink = (index) => {
  quickLinks.value.splice(index, 1)
}
</script>

<template>
  <div class="quick-links">
    <div class="links-header">
      <h2>快捷链接</h2>
      <button @click="showAddForm = !showAddForm" class="add-btn">
        {{ showAddForm ? '取消' : '添加链接' }}
      </button>
    </div>

    <!-- 添加链接表单 -->
    <div v-if="showAddForm" class="add-link-form">
      <div class="form-group">
        <input v-model="newLink.name" placeholder="链接名称" class="form-input" />
        <input v-model="newLink.url" placeholder="链接地址 (https://...)" class="form-input" />
        <button @click="addLink" class="add-link-btn">添加</button>
      </div>
    </div>

    <!-- 快捷链接网格 -->
    <div class="links-grid">
      <div 
        v-for="(link, index) in quickLinks" 
        :key="index" 
        class="link-item"
        @click="window.open(link.url, '_blank')"
      >
        <div class="link-icon">{{ link.icon }}</div>
        <div class="link-name">{{ link.name }}</div>
        <button @click.stop="removeLink(index)" class="remove-btn">×</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.quick-links {
  max-width: 800px;
  width: 100%;
}

.links-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.links-header h2 {
  color: white;
  font-size: 1.5rem;
  font-weight: 300;
  margin: 0;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
}

.add-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
  font-size: 0.9rem;
  white-space: nowrap;
}

.add-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.add-link-form {
  background: rgba(255, 255, 255, 0.1);
  padding: 1rem;
  border-radius: 10px;
  margin-bottom: 1.5rem;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.form-group {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.form-input {
  flex: 1;
  padding: 0.5rem;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.9);
  color: #333;
  font-size: 0.9rem;
  min-width: 0; /* 防止flex item溢出 */
}

.form-input:focus {
  outline: none;
  border-color: rgba(255, 255, 255, 0.5);
}

.add-link-btn {
  background: #4CAF50;
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 5px;
  cursor: pointer;
  font-size: 0.9rem;
  white-space: nowrap;
  transition: background-color 0.3s ease;
}

.add-link-btn:hover {
  background: #45a049;
}

.links-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 1rem;
}

.link-item {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  padding: 1rem;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  min-height: 80px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.link-item:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.link-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
  line-height: 1;
}

.link-name {
  color: white;
  font-size: 0.9rem;
  font-weight: 500;
  line-height: 1.2;
  word-break: break-word;
  text-align: center;
}

.remove-btn {
  position: absolute;
  top: 5px;
  right: 5px;
  background: rgba(255, 0, 0, 0.7);
  color: white;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 14px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.link-item:hover .remove-btn {
  opacity: 1;
}

/* 平板设备 (768px - 1024px) */
@media (max-width: 1024px) and (min-width: 769px) {
  .quick-links {
    max-width: 700px;
  }
  
  .links-header h2 {
    font-size: 1.3rem;
  }
  
  .add-btn {
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
  }
  
  .links-grid {
    grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
    gap: 0.8rem;
  }
  
  .link-item {
    padding: 0.8rem;
    min-height: 75px;
  }
  
  .link-icon {
    font-size: 1.8rem;
  }
  
  .link-name {
    font-size: 0.85rem;
  }
}

/* 移动设备 (768px 以下) */
@media (max-width: 768px) {
  .quick-links {
    max-width: 100%;
  }
  
  .links-header {
    margin-bottom: 1rem;
    flex-direction: column;
    gap: 0.8rem;
    align-items: stretch;
  }
  
  .links-header h2 {
    font-size: 1.2rem;
    text-align: center;
  }
  
  .add-btn {
    align-self: center;
    max-width: 120px;
  }
  
  .add-link-form {
    padding: 0.8rem;
    margin-bottom: 1rem;
  }
  
  .form-group {
    flex-direction: column;
    gap: 0.8rem;
  }
  
  .form-input {
    width: 100%;
    padding: 0.6rem;
  }
  
  .add-link-btn {
    width: 100%;
    padding: 0.6rem;
  }
  
  .links-grid {
    grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
    gap: 0.6rem;
  }
  
  .link-item {
    padding: 0.6rem;
    min-height: 70px;
    border-radius: 8px;
  }
  
  .link-icon {
    font-size: 1.6rem;
    margin-bottom: 0.3rem;
  }
  
  .link-name {
    font-size: 0.8rem;
  }
  
  .remove-btn {
    width: 18px;
    height: 18px;
    font-size: 12px;
  }
}

/* 小屏手机 (480px 以下) */
@media (max-width: 480px) {
  .links-header {
    gap: 0.6rem;
  }
  
  .links-header h2 {
    font-size: 1.1rem;
  }
  
  .add-btn {
    max-width: 100px;
    padding: 0.3rem 0.6rem;
    font-size: 0.8rem;
  }
  
  .add-link-form {
    padding: 0.6rem;
  }
  
  .form-group {
    gap: 0.6rem;
  }
  
  .form-input {
    padding: 0.5rem;
    font-size: 0.85rem;
  }
  
  .links-grid {
    grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
    gap: 0.5rem;
  }
  
  .link-item {
    padding: 0.5rem;
    min-height: 65px;
    border-radius: 6px;
  }
  
  .link-icon {
    font-size: 1.4rem;
  }
  
  .link-name {
    font-size: 0.75rem;
  }
  
  .remove-btn {
    width: 16px;
    height: 16px;
    font-size: 11px;
    top: 3px;
    right: 3px;
  }
}

/* 横屏手机优化 */
@media (max-height: 500px) and (orientation: landscape) {
  .quick-links {
    max-width: 90%;
  }
  
  .links-grid {
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    gap: 0.8rem;
  }
  
  .link-item {
    min-height: 60px;
    padding: 0.5rem;
  }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
  .link-item:hover {
    background: rgba(255, 255, 255, 0.1);
    transform: none;
  }
  
  .link-item:active {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(0.98);
  }
  
  .remove-btn {
    opacity: 0.7; /* 在触摸设备上始终显示删除按钮 */
  }
  
  .link-item:hover .remove-btn {
    opacity: 0.7;
  }
}
</style>
